package org.snail.attendence.controller;

import org.apache.log4j.Logger;
import org.snail.attendence.bean.*;
import org.snail.attendence.mapper.AttendenceMapper;
import org.snail.attendence.service.impl.AttendanceFileOutputServiceImpl;
import org.snail.attendence.service.impl.AttendanceListServiceImpl;
import org.snail.attendence.service.impl.AttendanceCheckServiceImpl;
import org.snail.attendence.service.impl.AttendenceMongoServiceImpl;
import org.snail.base.AttendenceCondition;
import org.snail.common.dingding.utils.JedisUtils;
import org.snail.constant.AttendenceConstant;
import org.snail.constant.ErrCodeConstant;
import org.snail.employee.service.impl.EmployeesServiceImpl;
import org.snail.exception.BaseException;
import org.snail.exception.CommonException;
import org.snail.exception.EmpException;
import org.snail.other.service.impl.SendMessageServiceImpl;
import org.snail.exception.JsonResult;
import org.snail.other.bean.GroupNotificationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by WJ on 2017/12/12.
 */
@Controller
@RequestMapping("attendence")
public class AttendenceController {
    public static final Logger LOGGER = Logger.getLogger(AttendenceController.class);
    @Autowired
    private AttendanceFileOutputServiceImpl attendanceFileOutputServiceImpl;
    @Autowired
    public SendMessageServiceImpl sendMessageService;
    @Autowired
    private AttendanceListServiceImpl attendanceListService;
    @Autowired
    private AttendenceMongoServiceImpl attendenceMongoService;
    @Autowired
    private AttendenceMapper attendenceMapper;
    @Autowired
    private AttendanceCheckServiceImpl finalAttendanceImpl;
    @Autowired
    private EmployeesServiceImpl employeesService;
    @Autowired
    private JedisUtils jedisUtils;



    /**
     * mongod --dbpath D:\MongoDB\Server\3.6\bin --port 27017
     * 返回页面所有的异常考勤记录
     * @param file 传入文件
     * @param request request中取数据
     * @return jsonResult 里面还有异常考勤列表
     */
    @RequestMapping("createAbnormalList")
    @ResponseBody
    public JsonResult createAbnormalList(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws BaseException, ParseException {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        List<AbnormalAttendanceDTO> attendanceList = attendanceFileOutputServiceImpl.getAttendanceList(file, request);
        jsonResult.setData(attendanceList);
        return jsonResult;
    }

//    合并到开发中


//    /**
//     *考勤异常数据发送至各个钉钉群
//     **/
//    @ResponseBody
//    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
//    public JsonResult sendMessage(AttendenceCondition attendenceCondition) throws EmpException, CommonException {
//        //json格式{"外包工号":"异常类型","外包工号":"异常类型",.........}
//        //第一种方式
////        sendMessageService.sendMessage(resultMap);
//        attendanceFileOutputServiceImpl.sendMessageToDing(attendenceCondition);
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode(ErrCodeConstant.SUCCESS);
//        jsonResult.setMessage("群消息发送成功,并将邮箱发送至相关人员处！");
//        return jsonResult;
//    }

    /**
     *考勤异常数据发送至各个钉钉群
     **/
    @ResponseBody
    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    public JsonResult sendMessage(@RequestBody List<AbnormalAttendanceDTO> abnormalAttendanceList) throws Exception {
        //json格式{"外包工号":"异常类型","外包工号":"异常类型",.........}
        //第一种方式
//        sendMessageService.sendMessage(resultMap);
        attendanceFileOutputServiceImpl.sendMessageToDing(abnormalAttendanceList);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        jsonResult.setMessage("群消息发送成功,并将邮箱发送至相关人员处！");
        return jsonResult;
    }


    @RequestMapping("/sendEmail")
    @ResponseBody
    public JsonResult sendEmail(){
        attendanceFileOutputServiceImpl.sendEmail();
        JsonResult result = new JsonResult();
        result.setMessage("消息发送成功！");
        result.setCode(ErrCodeConstant.SUCCESS);
        return result;
    }

    /**
     * 考勤预览
     *
     * @param
     * @return
     * @throws EmpException
     */

    @RequestMapping("/prevAttendance")
//    @RequestMapping("/saveAttendanceExcel")
    @ResponseBody
    public JsonResult prevAttendance(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws Exception {
        JsonResult result = new JsonResult();
        attendanceListService.saveAttendance(file,request);
        result.setMessage("保存考勤信息成功");
        return result;
    }

    /**
     * 查询正常考勤
     * @return
     */
    @RequestMapping("/findAllNormal")
    @ResponseBody
    public JsonResult findAllNormal(@RequestBody AttendanceCondition attendanceCondition){
        JsonResult result = new JsonResult();
        Map<String, Object> aliWorkDTOs = attendanceListService.findAllNormal(attendanceCondition);
        result.setData(aliWorkDTOs);
        result.setMessage("查询正常考勤成功");
        return result;
    }

    /**
     * 查询异常
     * @return
     */
    @RequestMapping("/findAllAbNormal")
    @ResponseBody
    public JsonResult findAllAbNormal(@RequestBody AttendanceCondition attendanceCondition){
        JsonResult result = new JsonResult();
        Map<String, Object> abnormalAttendanceDTOs = attendanceListService.findAllAbNormal(attendanceCondition);
        result.setData(abnormalAttendanceDTOs);
        result.setMessage("查询异常考勤成功");
        return result;
    }

    /**
     * 查询所有考勤
     * @return
     */
    @RequestMapping("/findAllAttendence")
    @ResponseBody
    public JsonResult findAllAttendence(){
        JsonResult jsonResult = new JsonResult();

        return jsonResult;
    }

//    /** 注释时间:2018-12-28
//     * 考勤保存至数据库
//     *
//     * @param
//     * @return
//     * @throws EmpException
//     */
//    @RequestMapping("/saveAttendance")
//    @ResponseBody
//    public JsonResult saveAttendance(@RequestBody List<AliWorkDTO> aliWorkDTOs) throws ParseException, CommonException {
//        JsonResult result = new JsonResult();
//        attendanceListService.saveAttendenceQueue(aliWorkDTOs);
//        return result;
//    }

    /**
     *  核算考勤
     * @return
     */
    @RequestMapping("/findAttendanceCheck")
    @ResponseBody
    public JsonResult findAttendanceCheck(AttendanceCondition attendanceCondition) throws ParseException {
        JsonResult result = new JsonResult();
        List<AttendanceCheckDO> attendanceCheckDOs = attendanceListService.findAttendanceCheckBySearch(attendanceCondition);
        result.setData(attendanceCheckDOs);
        result.setMessage("查询成功");
        return result;
    }

    /**
     *  考勤总计
     * @return
     */
    @RequestMapping("/findAttendanceCount")
    @ResponseBody
    public JsonResult findAttendanceCount(AttendanceCondition attendanceCondition) throws ParseException {
        JsonResult result = new JsonResult();
        List<AttendanceCheckDO> attendanceCheckDOs = attendanceListService.findAttendanceCount(attendanceCondition);
        result.setData(attendanceCheckDOs);
        result.setMessage("查询成功");
        return result;
    }


    /**
     * 考勤明细
     * @param attendanceCondition
     * @return
     */
    @RequestMapping("/findAttendanceDetail")
    @ResponseBody
    public JsonResult findAttendanceDetail(AttendanceCondition attendanceCondition) throws ParseException {
        JsonResult result = new JsonResult();
        List<AttendanceCheckDO> attendanceCheckDOs = attendanceListService.findAttendanceDetail(attendanceCondition);
        result.setData(attendanceCheckDOs);
        result.setMessage("查询成功");
        return result;
    }

    /**
     *  核算考勤
     *  保存部分字段 不可调休工作日，周末，法定节假日
     * @return
     */
    @RequestMapping("/saveAttendanceCheck")
    @ResponseBody
    public JsonResult saveAttendanceCheck(@RequestBody List<AttendanceCheckDO> attendanceCheckDOs) throws CommonException {
        JsonResult result = new JsonResult();
        attendanceListService.saveAttendanceCheck(attendanceCheckDOs);
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 导入EXCEL钉钉考勤数据展示
     * @return
     */
    @RequestMapping("/exhibitionAttendance")
    @ResponseBody
    public JsonResult exhibitionAttendance(AttendanceCondition attendanceCondition){
        JsonResult jsonResult = new JsonResult();
        List<AttendenceCommonDO> attendenceCommonDOS = attendanceListService.exhibitionAttendanceByConditionSearch(attendanceCondition);
        jsonResult.setData(attendenceCommonDOS);
        return jsonResult;
    }

    /**
     * 钉钉详情
     * @param attendenceCondition
     * @return
     */
    @RequestMapping("/detailsLetter")
    @ResponseBody
    public JsonResult detailsLetter(AttendenceCondition attendenceCondition){
        JsonResult jsonResult = new JsonResult();
        List list = attendanceListService.detailsLetter(attendenceCondition);
        jsonResult.setData(list);
        jsonResult.setMessage("钉钉考勤详情查询成功");
        return jsonResult;
    }

    @RequestMapping("/attendanceDownLoad")
    public ResponseEntity<byte[]> download() throws IOException {
        return attendanceFileOutputServiceImpl.download();
    }
}
